package com.sea.domain;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sea.service.SwapManager;
import com.sea.struct.InputDataStruct;
import com.sea.struct.data.InputShowData2;
import com.sea.utils.ByteUtils;
import com.sea.utils.StrUtils;
import com.sea.utils.StructUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class Struct {

    private static Logger logger = LoggerFactory.getLogger(Struct.class);

    // 起始标志 1 字节[0]
    public long startTag;

    // 数据长度 1 字节[1]
    public long dataLength;

    // 序列号域 2 字节[2][3]
    public long serialNum;

    // 加密标志 1 字节[4]
    public long encrypt;

    // 帧类型标志 1 字节[5]
    public long type;

    // 消息体(输入) N 字节
//    public DataStruct data;

    // 帧校验域 2 字节
    public long endTag;

    /**
     * 根据out输出流解析数据
     */
    public Map<String, Object> parserInput(ByteArrayOutputStream bs, String hostNum) throws Exception {
        Map<String, Object> map = new HashMap<>();
        byte[] bytes = bs.toByteArray();
        logger.info("\n接收报文数据：");

        // 转化为16进制
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            byte[] bts = new byte[1];
            bts[0] = bytes[i];
            sb.append(ByteUtils.byteToHex(bts) + "\t");
        }
        logger.info(sb.toString());
        this.type = StructUtils.type(bytes[1]);             // 功能码
        this.dataLength = StrUtils.dataLength(bytes[2]); //  数据长度 第三位表示业务字段长度
        byte[] bizData = new byte[Long.valueOf(dataLength).intValue()];

        int index = 0;
        for (int i = 3; i < bytes.length - 2; i++) {
            bizData[index] = bytes[i];
            index += 1;
        }
        Class<?> cls = SwapManager.INPUT_STRUCT_CLASS.get(this.type + 1);
        InputDataStruct inputData = (InputDataStruct) cls.getConstructor().newInstance();
        inputData.parseData(bizData);
        String jsonStr = JSON.toJSONString(inputData);
        logger.info("解析内容**************" + jsonStr + "************");

        ObjectMapper mapper = new ObjectMapper();

        InputShowData2 showData = mapper.readValue(jsonStr, InputShowData2.class);
        try {
            mapper.writeValue(new File("host_1_3.json"), showData);
            logger.info("---host_1_3文件写入成功----");
        } catch (IOException e) {
            System.out.println("JSON文件写入失败：" + e.getMessage());
        }
        return map;
    }

    public Map<String, Object> parserInput_(ByteArrayOutputStream bs, String hostNum) throws Exception {
        Map<String, Object> map = new HashMap<>();
        byte[] bytes = bs.toByteArray();
        logger.info("\n接收报文数据：");

        // 转化为16进制
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            byte[] bts = new byte[1];
            bts[0] = bytes[i];
            sb.append(ByteUtils.byteToHex(bts) + "\t");
        }
        logger.info(sb.toString());
        this.type = StructUtils.type(bytes[1]);             // 功能码
        this.dataLength = StrUtils.dataLength(bytes[2]); //  数据长度 第三位表示业务字段长度
        byte[] bizData = new byte[Long.valueOf(dataLength).intValue()];

        int index = 0;
        for (int i = 3; i < bytes.length - 2; i++) {
            bizData[index] = bytes[i];
            index += 1;
        }
        Class<?> cls = SwapManager.INPUT_STRUCT_CLASS.get(this.type + 1);
        InputDataStruct inputData = (InputDataStruct) cls.getConstructor().newInstance();
        inputData.parseData(bizData);
        String jsonStr = JSON.toJSONString(inputData);
        logger.info("解析内容**************" + jsonStr + "************");

        ObjectMapper mapper = new ObjectMapper();

        InputShowData2 showData = mapper.readValue(jsonStr, InputShowData2.class);
        try {
            mapper.writeValue(new File("host_2_3.json"), showData);
            logger.info("---host_2_3文件写入成功----");
        } catch (IOException e) {
            System.out.println("JSON文件写入失败：" + e.getMessage());
        }
        return map;
    }
}
